package cn.pzhuweb.controller;

import java.sql.*;
import cn.pzhuweb.pojo.*;
import cn.pzhuweb.util.DBUtil;

import java.util.HashMap;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TodoServlet
 */

public class JDBCServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public JDBCServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			// 3.编写SQL
			String username = request.getParameter("username");
			// String sql = "select * from user where name='"+username+"'";
			String sql = "select * from user where name=?";
			// 4.创建命令对象
			// Statement sta = con.createStatement();
			PreparedStatement sta = con.prepareStatement(sql);
			// 5.执行命令
			// ResultSet res = sta.executeQuery(sql);
			sta.setString(1, username);
			ResultSet res = sta.executeQuery();
			// 6.处理结果
			HashMap<String, User> map = new HashMap<>();// 保存结果集中的所有用户信息
			while (res.next() == true) {
				String name = res.getString(1);
				String psw = res.getString("password");
				User temp = new User(name, psw);
				map.put(name, temp);// 将这一行的user加入集合中
			}
			// 7. 释放资源
			DBUtil.close(res, sta, con);
			request.getSession().setAttribute("map", map);
			response.sendRedirect("form.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
